package com.tracker

import java.util.Date;
import java.util.List;
import com.commonProject.common.exception.ValidateException
import com.commonProject.common.classes.TimesDTO;
import com.commonProject.common.intefaces.ITimes

class TimesService {

	static transactional = true
	
	// ManagerService managerService
	ITimes timesServiceProxy
	
    def getlist(params) {
		
		println("TimesService Test")
		//println(params)
		Long times_id=Long.valueOf(params.get("id"))
		List<TimesDTO> el=null
		el=timesServiceProxy.getlist(times_id)
		
		return el.collect{[
			text : it.title,
			id: it.id	
		]}	
   }
	
	
	def getelement(params){
		println(params)
		Long times_id=Long.valueOf(params.get("id"))
		TimesDTO el=null
		println("tumes_id ="+times_id)
		el=timesServiceProxy.getelement(times_id)
		println("TimesService getelement")
		
		return[
			'success' : 'true',
			id : el.id,
			title : el.title,
			price : el.price,
			dateCreated : el.dateCreated]		
	}
	
	def deleteElement(params){
		println(params)
		println("TimesService DELETE")
		Long times_id=Long.valueOf(params.get("id"))
		TimesDTO el=null		
		try{
			el=timesServiceProxy.deleteElement(times_id)
		}catch(Exception ex){
			//println "-->EXCEPTION: ${ex}"
			println "No server connection"
			return [success:false]
		}		
		return [success:true]
	}
	
	def editElement(params){
		println("Times EDIT")
		Long times_id=Long.valueOf(params.get("id"))
		String times_title=params.get('editTimesTitle')
		Integer times_price=Integer.parseInt(params.get('editTimesPrice'))
		String times_dateCreated=params.get('editTimesDateCreated')
		
		try{
			def el=timesServiceProxy.editElement(times_id,times_title,times_price,times_dateCreated)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
/*
	def editElement(params){
		println("Times EDIT")
			
		def edit = Times.get(params.get('id'))
		edit.title = params.get('editTimesTitle')
		edit.price=Integer.parseInt(params.get('editTimesPrice'))
		
		if (params.get('editTimesDateCreated')){
			edit.dateCreated = new Date(params.get('editTimesDateCreated'))
		}else{
			edit.dateCreated = null
		}
		if(edit.save()){
			return['success':'true']
		}else{
			def err = edit.errors.allErrors.collect{[
				errorMessage:messageSource.getMessage(it, null)
			]}
			println(err)
			return [
				'success':'false',
				errors:err				
				]
		}
	}*/
	
	def addElement(params) {
		println("ADD TUR")
		println(params)
				
		Long times_id=Long.valueOf(params.get('id'))
		String times_title=params.get('title')
		Integer times_price=Integer.parseInt(params.get('price'))
	
		try{
			def el=timesServiceProxy.addElement(times_id,times_title,times_price)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
	
	
}
